package com.jurassic.demo;

import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;

/**
 * @Author pan_jin
 * @Description: $
 * @Date: 2022/7/28 9:52
 * @Version: 1.0.0
 * @Slogan: 梦想还是要有的万一实现了呢
 */
public class SendMail {

    public static String myEmailAccount = "panjin162020@163.com";
    public static String myEmailPassword = "KERTFPQMHMXIAVXF";
    public static String myEmailSmtpHost = "smtp.163.com";
    public static String receiveMailAccount = "475405660@qq.com";

    public static void main(String[] args) {
        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.smtp.host", myEmailSmtpHost);
        props.setProperty("mail.smtp.auth", "true");
        Session session = Session.getInstance(props);
        session.setDebug(true);
        MimeMessage message = createMimeMessage(session, myEmailAccount, receiveMailAccount);
        Transport transport = null;
        try {
            transport = session.getTransport();
            transport.connect(myEmailAccount, myEmailPassword);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static MimeMessage createMimeMessage(Session session, String sendMail, String receiveMail) {
        MimeMessage message = new MimeMessage(session);
        try {

            message.setFrom(new InternetAddress(sendMail, "某宝网", "UTF-8"));
            message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMail, "XX用户", "UTF-8"));
            message.setSubject("打折钜惠", "UTF-8");
            message.setContent("XX用户你好, 今天全场5折, 快来抢购, 错过今天再等一年。。。", "text/html;charset=UTF-8");
            message.setSentDate(new Date());
            message.saveChanges();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return message;
    }
}
